/*
 *  Copyright 2011 yura.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package org.opu.vcforms.editor;

import java.awt.BorderLayout;
import java.awt.Component;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.swing.JFrame;

import javax.swing.JPanel;
import javax.swing.JScrollPane;

/**
 * 
 * @author yura
 */
public class SingletonEditView extends JPanel implements EditView {

    private static final long serialVersionUID = 7742108062397264960L;
    private PropertyChangeListener changeListener;
    private JFrame frame;

    public SingletonEditView() {
        super(new BorderLayout());
    }

    @Override
    public JFrame getFrame() {
        return frame;
    }

    @Override
    public TextView getSelectedText() {
        try {
            return (TextView) ((JScrollPane) getComponent(0)).getViewport().getComponent(0);
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public Iterator<TextView> iterator() {
        List<TextView> lst = new ArrayList<TextView>();
        try {
            lst.add((TextView)((JScrollPane)getComponent(0)).getViewport().getComponent(0));
        } catch (Exception e) {
        }
        return lst.iterator();
    }

    @Override
    protected void addImpl(Component comp, Object constraints, int index) {
        super.addImpl(comp, constraints, index);
        setChangeList(comp, this);
    }

    @Override
    public boolean isAllSaved() {
        return getSelectedText().isSaved();
    }

    @Override
    public void setChangeListener(PropertyChangeListener changeListener) {
        this.changeListener = changeListener;
        for (int i = 0; i < getComponentCount(); i++) {
            setChangeList(getComponent(i), this);
        }
    }

    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        if (changeListener != null) {
            changeListener.propertyChange(evt);
        }
    }

    @Override
    public PropertyChangeListener getChangeListener() {
        return changeListener;
    }

    private void setChangeList(Component component, PropertyChangeListener lst) {
        if (component instanceof JScrollPane) {
            Component c = ((JScrollPane) component).getViewport().getComponent(0);
            setChangeList(c, lst);
        } else {
            if (component instanceof TextView) {
                ((TextView) component).setChangeListener(this);
            } else {
                component.addPropertyChangeListener(this);
            }
        }
    }

    @Override
    public void setFrame(JFrame frame) {
        this.frame = frame;
    }
}
